package qdu.edu.com.fushanf4.service.sort;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 用来对IntWritable进行排序
 * Key: IntWritable
 * Value: Text
 */
public class IntWritableSortReducer extends Reducer<IntWritable, Text, Text, IntWritable> {
    @Override
    protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        for ( Text keyOut: values) {
            context.write(keyOut, key);
        }
    }

    public static class IntWritableSortComparator extends IntWritable.Comparator {
        @Override
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            return -super.compare(b1, s1, l1, b2, s2, l2);  // 添加一个负号，让升序变为降序
        }
    }
}
